package com.bruce.practice.dingding;


import cn.hutool.http.HttpUtil;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;

/**
 * @author luotuan
 * @Description
 * @create 2020-08-03 21:38
 **/
public class DingMsgSend {
    public static String url = "https://oapi.dingtalk.com/robot/send?access_token=ec54f903f74d35a0935a5397c65e3af68ec8933c6dc4fca04c847d7ffeef522e";
    public static void main(String[] args) throws Exception{
        Long timestamp = System.currentTimeMillis();
        String secret = "SECc8daa2b4f1ceb8b00297c635af70be899440af9c1033a677fe6b37c516f17315";
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
        String targetUrl = url+"&timestamp="+timestamp+"&sign="+sign;
        System.out.println("钉钉url："+targetUrl);
        String content = "测试";
        String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + content + "\"}}";
        System.out.println(HttpUtil.post(targetUrl,textMsg));
//        System.out.println(System.currentTimeMillis());
    }
}
